﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace FerrSimplicity
{
    /// <summary>
    /// This component will wrap this object around the edges of the screen.
    /// </summary>
    public class ComScreenWrap : BaseComponent
    {
        #region Fields
        int mWidth;
        int mHeight;
        #endregion

        #region Overrides
        public override void Enable(bool     aEnableValue)
        {
            if (aEnableValue)
            {
                // get a handle on the image component that may be attached to the object
                ComSprite2D image = null;
                image = Parent.GetComponent<ComSprite2D>();

                mWidth  = 0;
                mHeight = 0;

                // if there is an image attached to this object, use its width and height
                if (image != null)
                {
                    mWidth  = image.Width;
                    mHeight = image.Height;
                }
            }
            base.Enable(aEnableValue);
        }
        public override void Update(GameTime aGameTime   )
        {
            Vector2   translation = new Vector2();
            Rectangle bounds      = ManagerCameras.Instance.Bounds;

            // wrap it around!
            if (Parent.Transform.Position.X < bounds.Left + (-mWidth  / 2.0f))
                translation.X += bounds.Width + mWidth;
            if (Parent.Transform.Position.Y < bounds.Top  + (-mHeight / 2.0f))
                translation.Y += bounds.Height + mHeight;
            if (Parent.Transform.Position.X > (bounds.Right + mWidth / 2.0f))
                translation.X -= bounds.Width + mWidth;
            if (Parent.Transform.Position.Y > (bounds.Bottom + mHeight / 2.0f))
                translation.Y -= bounds.Height + mHeight;

            // update the position
            Parent.Transform.Position2D += translation;

            base.Update(aGameTime);
        }
        #endregion
    }
}
